package org.come.thread;

import com.tool.ModerateTask.*;
import com.tool.PlayerKill.PKSys;
import com.tool.image.ImageMixDeal;
import com.tool.image.ManimgAttribute;
import com.tool.imagemonitor.*;
import com.tool.role.RoleData;
import com.tool.tcp.SpriteFactory;
import com.tool.time.TimeLimit;
import come.tool.Fighting.FightingMixDeal;
import come.tool.handle.HandleState;
import come.tool.map.XLPath;
import org.come.Frame.AntiPluginJframe;
import org.come.Frame.NPCJfram;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.ZhuJpanel;
import org.come.action.MapAction;
import org.come.bean.RoleShow;
import org.come.control.JirenjiaobenchuliControl;
import org.come.model.Door;
import org.come.mouslisten.Mouselistener;
import org.come.npc.TP;
import org.come.until.*;

import java.util.ArrayList;
import java.util.List;

public class TimeControlRunnableNew implements Runnable
{
    private int jg=0;
    private int tx=0;
    private int dk=0;
    public static int JBTime=0;//防脚本
    private static boolean isScript=false;
    private static Object object=new Object();
    public TimeControlRunnableNew() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
//    	int dk = 0;
//        while (true) {
//            if(FightingMixDeal.State != HandleState.USUAL){
//                continue;
//            }
//            try {
//                long x=System.currentTimeMillis();
//                int cha=25;
//                if (FightingMixDeal.State==0){
//                    tx=6;
//                    cha=150;
//                }else {
//                    tx++;
//                    FightingMixDeal.PalyProgress(25);
//                }
//                if (tx>5) {
//                    tx=0;
//                    CommonUse();
//                }
//
//                x=System.currentTimeMillis()-x;
//                if (x<cha){
//                    Thread.sleep(cha-x);
//                }
//                Util.addtime(cha);
//            } catch (InterruptedException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
    }
    /**通用处理*/
    public void CommonUse(){
        jg++;
        if (JBTime>0) {
            JBTime-=150;
            if (JBTime<=0) {
                AntiPluginJframe.getAntiPluginJframe().getPluginJpanel().overtime();
            }
        }
        // 画出组队申请
        if (ZhuFrame.getZhuJpanel().getTeamState() == 1){
            ZhuJpanel.getGroupBtn().imgchange((ZhuJpanel.getGroupBtn().getImgzhen() + 1) % 2);
        }
        // 画出好友消息friends
        if (MessagrFlagUntil.NotReads.size() > 0){
            ZhuJpanel.getFriendsBtn().imgchange((ZhuJpanel.getFriendsBtn().getImgzhen() + 1) % 2);
        }
        if (isScript&&jg%4==0) {
            scriptHandle();
        }
        if (jg%30==0) {
            TimeLimit.Timeout();
        }else if (jg>500) {
            jg=0;
            // 判断宝宝状态
            if (RoleData.getRoleData().getLoginResult().getHavebaby() > 0){
                BabyUntil.calculateBaby();
            }
            Hero.getHero().OverTime();
            PKSys.getPkSys().isexpiation();
            SpriteFactory.ResetAndRemove();
            CutButtonImage.removeNPCHeadImg();
            for (int i = ImageMixDeal.npcimglist.size()-1; i >=0 ; i--) {
                ImageMixDeal.npcimglist.get(i).npcmsg();
            }
            GoodsListFromServerUntil.isSendPackRecord();
        }
    }
    /**脚本处理*/
    public void scriptHandle(){
        synchronized (object) {
            int size=script.size();
            if (size==0) {
                isScript=false;
                return;
            }
            size-=1;
            Object object=script.get(size);
            if (object==null) {script.remove(size);return;}


            if (object instanceof ScriptOpen) {
                //0寻路  1点击NPC  2点击野怪  3选择传送选项  4是点击自定义野怪
                ScriptOpen open=(ScriptOpen) object;
                if (open.getType()==0) {
                    Mouselistener.XL(open.getX()/20, open.getY()/20);
                }else if (open.getType()==1) {
                    ManimgAttribute attribute=ImageMixDeal.getNpc(open.getNpc()+"");
                    if (attribute!=null) {
                        NpcMonitor.npc(attribute);
                    }
                }else if (open.getType()==2) {
                    ManimgAttribute attribute=ImageMixDeal.getTask(open.getNpc());
                    if (attribute!=null) {
                        TaskMonitor.TaskCreeps(attribute.getTaskdata());
                    }
                }else if (open.getType()==3) {
                    Door door=UserMessUntil.getDoor(open.getDoor()+"");
                    if (door!=null) {
                        TP.tp(door, 3);
                    }
                    FormsManagement.HideForm(27);
                }else if (open.getType()==101) {//传送指定位置
                    TP.tp(open.getDoor(),open.getX(),open.getY());
                }else if (open.getType()==102) {//领取任务id
                    if(NPCJfram.getNpcJfram().getNpcjpanel().getNpcInfoBean() != null){
                        TaskMixDeal.isoption(open.getNpc(),null,NPCJfram.getNpcJfram().getNpcjpanel().getNpcInfoBean().getNpctable().getNpcway());
                        FormsManagement.HideForm(27);
                    }

                }else if (open.getType()==103) {//攻击NPC 或者 任务怪
                    MapAction.npcmenuAction.get(NpcMenuUntil.getMenu48()).menuControl("我是来击杀你的");
                    FormsManagement.HideForm(27);
                }
                script.remove(size);
            }else if (object instanceof ScriptEnd) {
                ScriptEnd end=(ScriptEnd) object;
                if (end.getType()==0) {
                    RoleShow show=ImageMixDeal.userimg.getRoleShow();
                    if (JirenjiaobenchuliControl.name!=null){
                        if (ImageMixDeal.Playerimgmap.get(JirenjiaobenchuliControl.name)!=null)
                            show=ImageMixDeal.Playerimgmap.get(JirenjiaobenchuliControl.name).getRoleShow();
                    }
                    if (show.getMapid().intValue()==end.getMap()&&Math.abs(show.getX()-end.getX())<100&&Math.abs(show.getY()-end.getY())<100) {
                        script.remove(size);
                        scriptTask.getActiveBase().setTotal(scriptTask.getTotal()+1);
                        scriptTask.setTotal(scriptTask.getTotal()+1);
                    }
                }
            }
        }
    }
    public static List<Object> script;
    private static ScriptTask scriptTask;//任务处理
    public static void addTask(ScriptTask scriptTask){
        if(scriptTask.getStartTotal() <= scriptTask.getTotal()){
            removeScript();
            return;
        }
        synchronized (object) {
            removeScript();//清理之前的脚本纪录
            TimeControlRunnableNew.scriptTask=null;//清空之前的自动任务模式
            Task task=Hero.getHero().getSetTask(scriptTask.getTaskSetID());//根据任务系列id 获取人物身上的任务
            if (task!=null) {//如果人物身上有这个任务
                TimeControlRunnableNew.scriptTask=scriptTask;//set 自动任务模式
                upTask(task, task.getTaskState());//调用自动任务模式处理
            }else {//如果人物身上没有这个任务
                RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();

                if(roleShow.getMapid() != scriptTask.getMap() || roleShow.getX() != scriptTask.getX()+100 || roleShow.getY() != scriptTask.getY()+100){
                    TP.tp(scriptTask.getMap(), scriptTask.getX()+100, scriptTask.getY()+100);
                }

                //生成脚本 前往领取任务
                roleShow =ImageMixDeal.userimg.getRoleShow();
                if (JirenjiaobenchuliControl.duizhang!=null){
                    roleShow=JirenjiaobenchuliControl.duizhang.getRoleShow();
                }

                List<Object> list=XLPath.ZDXL(roleShow.getX(),roleShow.getY(),roleShow.getMapid().intValue(),scriptTask.getX(),scriptTask.getY(),scriptTask.getMap());
                if (list==null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                    return;
                }
                ScriptOpen open=new ScriptOpen(1);
                open.setNpc(scriptTask.getNpcID());
                list.add(0,open);

                ScriptOpen openTwo=new ScriptOpen(102);
                openTwo.setNpc(scriptTask.getTaskSetID());
                list.add(0,openTwo);

                TimeControlRunnableNew.scriptTask=scriptTask;
                TimeControlRunnableNew.addScript(list);
            }
        }
    }
    public static void upTask(Task task,int state){
        synchronized (object) {
            //自动任务模式为空 或者 自动任务模式系列id 不同 退出
            if (scriptTask==null||scriptTask.getTaskSetID()!=task.getTaskData().getTaskSetID()) {return;}
            //状态是应超时取消任务  或者 取消任务   清空自动任务模式  退出
            if (state==TaskState.quxiaoTime||state==TaskState.quxiao) {scriptTask=null;return;}
            //如果你是队员  清空自动任务模式  退出
            if (JirenjiaobenchuliControl.name==null&&ImageMixDeal.userimg.getTeams()==null) {scriptTask=null;return;}
            if (state==TaskState.finishTask) {//完成任务
                //回归接下一个任务  生成脚本
                List<Object> list=new ArrayList<>();
                ScriptOpen openDoor=new ScriptOpen(101);
                openDoor.setDoor(scriptTask.getMap());
                openDoor.setX(scriptTask.getX());openDoor.setY(scriptTask.getY());
                list.add(0,openDoor);

                ScriptEnd endDoor=new ScriptEnd(0,openDoor.getDoor(),openDoor.getX(),openDoor.getY());
                list.add(0,endDoor);
                list.add(null);list.add(null);//卡2个执行判断
                if (scriptTask.upSum()<0) {
                    scriptTask=null;
                    TimeControlRunnableNew.addScript(list);
                    return;
                }
                ScriptOpen open=new ScriptOpen(1);
                open.setNpc(scriptTask.getNpcID());
                list.add(0,open);

                ScriptOpen openTwo=new ScriptOpen(102);
                openTwo.setNpc(scriptTask.getTaskSetID());
                list.add(0,openTwo);

                TimeControlRunnableNew.addScript(list);
            }else {//判断任务条件 生成脚本
                TaskProgress progress=task.getTaskProgress();
                if (progress!=null&&progress.getMap()!=0) {

                    RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();

                    if(roleShow.getMapid() != progress.getMap() || progress.getMax()+100 != roleShow.getX() || progress.getY()+100 != roleShow.getY()){
                        TP.tp(progress.getMap(), progress.getX()+100, progress.getY()+100);
                    }

                    roleShow =ImageMixDeal.userimg.getRoleShow();

                    List<Object> list=XLPath.ZDXL(roleShow.getX(),roleShow.getY(),roleShow.getMapid().intValue(),progress.getX(),progress.getY(),progress.getMap());
                    if (list==null) {
                        ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                        return;
                    }
                    ScriptOpen open=new ScriptOpen(progress.getType()==1?2:1);
                    open.setNpc(progress.getDId());
                    list.add(0, open);
                    if (progress.getType()==1||progress.getType()==2) {
                        //击杀NPC
                        ScriptOpen openTwo=new ScriptOpen(103);
                        openTwo.setNpc(progress.getDId());
                        list.add(0,openTwo);
                    }
                    TimeControlRunnableNew.addScript(list);
                }
            }
        }
    }
    //接取任务  寻路  执行任务 任务完成 传送回去  有剩余的任务执行次数 回到第一条
    /**添加脚本*/
    public static void addScript(List<Object> list){
        synchronized (object) {
            script=list;
            isScript=true;
        }
    }
    /**取消脚本*/
    public static void removeScript(){
        synchronized (object) {
            script=null;
            isScript=false;
        }
    }
}